# =============================================================================
# PowerShell Profile Configuration
# =============================================================================

# PSReadLine Configuration
Set-PSReadLineOption -EditMode Emacs
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -ShowToolTips
Set-PSReadLineOption -BellStyle None
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete

# =============================================================================
# Aliases
# =============================================================================
Set-Alias g lazygit
Set-Alias l y
Set-Alias v nvim
Set-Alias vim nvim
Set-Alias which where.exe
Set-Alias grep findstr

# =============================================================================
# Environment Variables
# =============================================================================
$env:POWERSHELL_UPDATECHECK = 'Off'
$env:EDITOR = "nvim"
$env:VISUAL = "nvim"

# =============================================================================
# Basic Functions
# =============================================================================
function clear { cls }
function ll { dir $args }
function ifconfig { ipconfig $args }
function type { type $args }
function open { explorer $args }

# =============================================================================
# File Manager Functions
# =============================================================================
function lfcd {
    $lastDir = & lf -print-last-dir $args
    if ($lastDir -ne $null) {
        Set-Location -Path $lastDir
    }
}

#f45873b3-b655-43a6-b217-97c00aa0db58 PowerToys CommandNotFound module

# Import-Module -Name Microsoft.WinGet.CommandNotFound
#f45873b3-b655-43a6-b217-97c00aa0db58

$env:POWERSHELL_UPDATECHECK = 'Off'
Set-Item -Path Env:POWERSHELL_UPDATECHECK -Value 'Off'

$env:EDITOR = "nvim"
$env:VISUAL = "nvim"

# $env:TERM = "xterm-256color"





function y {
    $tmp = [System.IO.Path]::GetTempFileName()
    yazi $args --cwd-file="$tmp"
    $cwd = Get-Content -Path $tmp
    if (-not [String]::IsNullOrEmpty($cwd) -and $cwd -ne $PWD.Path) {
        Set-Location -LiteralPath $cwd
    }
    Remove-Item -Path $tmp
}


function goto-note {
    Set-Location -Path "$HOME\note\sigma"
}
Set-Alias -Name oo -Value goto-note

function goto-dotfile {
    Set-Location -Path "$HOME\dotfile"
}
Set-Alias -Name dot -Value goto-dotfile



# =============================================================================
# Network Functions
# =============================================================================
function Set-Proxy {
    [CmdletBinding()]
    param (
        [string]$HostIp = "127.0.0.1",
        [int]$Port = 7897,
        [string]$Comment = "Clash Verge"
    )

    $proxyUrl = "http://${HostIp}:$Port"
    $env:http_proxy = $proxyUrl
    $env:https_proxy = $proxyUrl
    $env:HTTP_PROXY = $proxyUrl
    $env:HTTPS_PROXY = $proxyUrl

    Write-Host "--[ Set Network Proxy! Value: HostIp = $HostIp, Port = $Port ($Comment) ]--" -ForegroundColor Green
}

function Remove-Proxy {
    [CmdletBinding()]
    param()

    $proxyVars = @('http_proxy', 'https_proxy', 'HTTP_PROXY', 'HTTPS_PROXY')
    foreach ($var in $proxyVars) {
        Remove-Item "Env:$var" -ErrorAction SilentlyContinue
    }
    Write-Host "--[ Remove Network Proxy! ]--" -ForegroundColor Yellow
}


# =============================================================================
# Zoxide Integration
# =============================================================================
if (Get-Command -Name zoxide -ErrorAction SilentlyContinue) {
    try {
        # 初始化 zoxide
        $hook = if ($PSVersionTable.PSVersion.Major -lt 6) { 'prompt' } else { 'pwd' }
        $zoxideInit = zoxide init --hook $hook powershell 2>$null
        if ($zoxideInit) {
            Invoke-Expression ($zoxideInit -join "`n")
        }

        # 把 cd 映射成 z（只在交互式会话里做，避免脚本里出问题）
        if ($Host.Name -match 'ConsoleHost|Visual Studio Code Host') {
            Set-Alias -Name cd -Value z -Option AllScope -Scope Global -Force
            Write-Verbose "Zoxide initialized and cd alias set"
        }
    }
    catch {
        Write-Warning "Failed to initialize zoxide: $($_.Exception.Message)"
    }
} else {
    Write-Host '[zoxide] 未检测到 zoxide，请先安装并加入 PATH。' -ForegroundColor DarkYellow
}


# =============================================================================
# Import Custom Modules
# =============================================================================
$modulePath = Join-Path $HOME "Documents\PowerShell" # 假设模块放在这里
Import-Module (Join-Path $modulePath "custom_utils.psm1")


